package com.asa.voice.mp3.asa.decoder;

public class ID3Tag {
	  private byte[] apicData;
	  
	  private String title;
	  
	  private String artist;
	  
	  private String album;
	  
	  private String year;
	  
	  private int version;
	  
	  private int exheaderSize;
	  
	  private boolean haveID3v2Footer;
	  
	  private String[] TEXT_ENCODING = new String[] { "GBK", "UTF-16", "UTF-16BE", "UTF-8" };
	  
	  private int apeVer;
	  
	  public void printTag() {
	    if (this.title != null)
	      System.out.println("      [ Title] " + this.title); 
	    if (this.artist != null)
	      System.out.println("      [Artist] " + this.artist); 
	    if (this.album != null)
	      System.out.println("      [ Album] " + this.album); 
	    if (this.year != null)
	      System.out.println("      [  Year] " + this.year); 
	  }
	  
	  public void clear() {
	    this.title = this.artist = this.album = this.year = null;
	    this.version = this.exheaderSize = 0;
	    this.haveID3v2Footer = false;
	    this.apicData = null;
	  }
	  
	  public void settTitle(String paramString) {
	    this.title = paramString;
	  }
	  
	  public String getTitle() {
	    return this.title;
	  }
	  
	  public void settArtist(String paramString) {
	    this.artist = paramString;
	  }
	  
	  public String getArtist() {
	    return this.artist;
	  }
	  
	  public String getAlbum() {
	    return this.album;
	  }
	  
	  public String getYear() {
	    return this.year;
	  }
	  
	  public byte[] getPicture() {
	    return this.apicData;
	  }
	  
	  public boolean checkID3V1(byte[] paramArrayOfbyte, int paramInt) {
	    return (paramArrayOfbyte[paramInt] == 84 && paramArrayOfbyte[paramInt + 1] == 65 && paramArrayOfbyte[paramInt + 2] == 71);
	  }
	  
	  public void parseID3V1(byte[] paramArrayOfbyte, int paramInt) {
	    if (paramArrayOfbyte.length < 128 || !checkID3V1(paramArrayOfbyte, paramInt))
	      return; 
	    byte[] arrayOfByte = new byte[125];
	    System.arraycopy(paramArrayOfbyte, 3 + paramInt, arrayOfByte, 0, 125);
	    byte b;
	    for (b = 0; b < 30 && arrayOfByte[b] != 0; b++);
	    try {
	      if (this.title == null)
	        this.title = new String(arrayOfByte, 0, b, "gbk"); 
	      if (this.title.length() == 0)
	        this.title = null; 
	      for (b = 30; b < 60 && arrayOfByte[b] != 0; b++);
	      if (this.artist == null)
	    	  
//	        this.artist = (new String(arrayOfByte, 30, b - 30, "gbk")).replaceAll("[^", ""); 
	    	this.artist = (new String(arrayOfByte, 30, b - 30, "gbk"));
	      
	      if (this.artist.length() == 0)
	        this.artist = null; 
	      for (b = 60; b < 90 && arrayOfByte[b] != 0; b++);
	      if (this.album == null)
	        this.album = new String(arrayOfByte, 60, b - 60, "gbk"); 
	      if (this.album.length() == 0)
	        this.album = null; 
	      for (b = 90; b < 94 && arrayOfByte[b] != 0; b++);
	      if (this.year == null)
	        this.year = (new String(arrayOfByte, 90, b - 90, "gbk")).replaceAll("[^0-9]", ""); 
	      if (this.year.length() == 0)
	        this.year = null; 
	    } catch (Exception exception) {
	      exception.printStackTrace();
	    } 
	    arrayOfByte = null;
	  }
	  
	  public int checkID3V2(byte[] paramArrayOfbyte, int paramInt) {
	    if (paramArrayOfbyte.length - paramInt < 10)
	      return 0; 
	    if (paramArrayOfbyte[paramInt] != 73 || paramArrayOfbyte[paramInt + 1] != 68 || paramArrayOfbyte[paramInt + 2] != 51)
	      return 0; 
	    this.version = paramArrayOfbyte[paramInt + 3] & 0xFF;
	    if (this.version > 2 && (paramArrayOfbyte[paramInt + 5] & 0x40) != 0)
	      this.exheaderSize = 1; 
	    this.haveID3v2Footer = ((paramArrayOfbyte[paramInt + 5] & 0x10) != 0);
	    int i = synchSafeInt(paramArrayOfbyte, paramInt + 6);
	    i += 10;
	    return i;
	  }
	  
	  public void parseID3V2(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
	    int i = paramInt1 + paramInt2;
	    int j = paramInt1 + 10;
	    if (this.exheaderSize == 1) {
	      this.exheaderSize = synchSafeInt(paramArrayOfbyte, paramInt1);
	      j += this.exheaderSize;
	    } 
	    i -= 10;
	    if (this.haveID3v2Footer)
	      i -= 10; 
	    while (j < i)
	      j += getFrame(paramArrayOfbyte, j, i); 
	  }
	  
	  private int synchSafeInt(byte[] paramArrayOfbyte, int paramInt) {
	    int i = (paramArrayOfbyte[paramInt] & Byte.MAX_VALUE) << 21;
	    i |= (paramArrayOfbyte[paramInt + 1] & Byte.MAX_VALUE) << 14;
	    i |= (paramArrayOfbyte[paramInt + 2] & Byte.MAX_VALUE) << 7;
	    i |= paramArrayOfbyte[paramInt + 3] & Byte.MAX_VALUE;
	    return i;
	  }
	  
	  private int byte2int(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
	    int i = paramArrayOfbyte[paramInt1] & 0xFF;
	    for (byte b = 1; b < paramInt2; b++) {
	      i <<= 8;
	      i |= paramArrayOfbyte[paramInt1 + b] & 0xFF;
	    } 
	    return i;
	  }
	  
	  private int getFrame(byte[] paramArrayOfbyte, int paramInt1, int paramInt2) {
	    int j, k, i = 4;
	    byte b = 10;
	    if (this.version == 2) {
	      i = 3;
	      b = 6;
	    } 
	    String str = new String(paramArrayOfbyte, paramInt1, i);
	    paramInt1 += i;
	    if (this.version <= 3) {
	      j = k = byte2int(paramArrayOfbyte, paramInt1, i);
	    } else {
	      j = k = synchSafeInt(paramArrayOfbyte, paramInt1);
	    } 
	    if (j < 0)
	      return b; 
	    paramInt1 += i;
	    if (this.version > 2)
	      paramInt1 += 2; 
	    byte b1 = paramArrayOfbyte[paramInt1];
	    k--;
	    paramInt1++;
	    if (k <= 0 || paramInt1 + k > paramInt2 || b1 < 0 || b1 >= this.TEXT_ENCODING.length)
	      return j + b; 
	    try {
	      String str1;
	      int m;
	      switch (str.hashCode()) {
	        case 83378:
	        case 2575251:
	          if (this.title == null)
//	            this.title = (new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1])).replaceAll("[^", ""); 
	        	this.title = (new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1]));
	          break;
	        case 83552:
	        case 2590194:
	          if (this.year == null)
	            this.year = (new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1])).replaceAll("[^0-9]", ""); 
	          break;
	        case 82815:
	        case 2567331:
	          if (this.album == null)
//	            this.album = (new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1])).replaceAll("[^", ""); 
	        	this.album = (new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1]));
	          break;
	        case 83253:
	        case 2581512:
	          if (this.artist == null) {
	            this.artist = new String(paramArrayOfbyte, paramInt1, k, this.TEXT_ENCODING[b1]);
//	            this.artist = this.artist.split("[^)[0];
	          } 
	          break;
	        case 2015625:
	          for (i = paramInt1; paramArrayOfbyte[i] != 0 && i < paramInt2; i++);
	          str1 = new String(paramArrayOfbyte, paramInt1, i - paramInt1, this.TEXT_ENCODING[b1]);
	          System.out.println("[APIC MIME type] " + str1);
	          k -= i - paramInt1 + 1;
	          paramInt1 = i + 1;
	          m = paramArrayOfbyte[paramInt1] & 0xFF;
	          System.out.println("[APIC Picture type] " + m);
	          paramInt1++;
	          k--;
	          for (i = paramInt1; paramArrayOfbyte[i] != 0 && i < paramInt2; i++);
	          System.out.println("[APIC Description] " + new String(paramArrayOfbyte, paramInt1, i - paramInt1, this.TEXT_ENCODING[b1]));
	          k -= i - paramInt1 + 1;
	          paramInt1 = i + 1;
	          if (paramArrayOfbyte[paramInt1] == 0) {
	            k--;
	            paramInt1++;
	          } 
	          if (this.apicData == null) {
	            this.apicData = new byte[k];
	            System.arraycopy(paramArrayOfbyte, paramInt1, this.apicData, 0, k);
	          } 
	          break;
	      } 
	    } catch (Exception exception) {}
	    return j + b;
	  }
	  
	  private int apeInt32(byte[] paramArrayOfbyte, int paramInt) {
	    if (paramArrayOfbyte.length - paramInt < 4)
	      return 0; 
	    return (paramArrayOfbyte[paramInt + 3] & 0xFF) << 24 | (paramArrayOfbyte[paramInt + 2] & 0xFF) << 16 | (paramArrayOfbyte[paramInt + 1] & 0xFF) << 8 | paramArrayOfbyte[paramInt] & 0xFF;
	  }
	  
	  public int checkAPEtagFooter(byte[] paramArrayOfbyte, int paramInt) {
	    if (paramArrayOfbyte.length - paramInt < 32)
	      return 0; 
	    if (paramArrayOfbyte[paramInt] == 65 && paramArrayOfbyte[paramInt + 1] == 80 && paramArrayOfbyte[paramInt + 2] == 69 && paramArrayOfbyte[paramInt + 3] == 84 && paramArrayOfbyte[paramInt + 4] == 65 && paramArrayOfbyte[paramInt + 5] == 71 && paramArrayOfbyte[paramInt + 6] == 69 && paramArrayOfbyte[paramInt + 7] == 88) {
	      this.apeVer = apeInt32(paramArrayOfbyte, paramInt + 8);
	      return apeInt32(paramArrayOfbyte, paramInt + 12) + 32;
	    } 
	    return 0;
	  }
	  
	  public int getApeVer() {
	    return this.apeVer;
	  }
}

